home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / cport1.zip / CPORT.DOC < prev    next >
Text File  |  1991-06-02  |  79KB  |  3,535 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.                 Cport 
  11.  
  12.  
  13.                 C Language Serial Communications Library                
  14.  
  15.                 Version 1.10
  16.  
  17.                 Copyright (c) 1991  Bri Productions
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.                                        1   
  63.  
  64.  
  65.  
  66.         Table of Contents
  67.         
  68.  
  69.         Introduction
  70.  
  71.                 Acknowledgments..........................5
  72.  
  73.                 General Description......................5
  74.  
  75.                 Disclaimer...............................5
  76.  
  77.                 Registration.............................5
  78.  
  79.                 Contacting the Author....................6
  80.  
  81.                 A typedef................................6
  82.  
  83.                 "Remote Device"..........................6
  84.  
  85.         
  86.         Control Functions
  87.  
  88.                 Description..............................7
  89.  
  90.                 ComOpen..................................8
  91.  
  92.                 ComOpenS................................11
  93.                                                 
  94.                 ComReopen...............................14
  95.  
  96.                 ComClose................................14
  97.  
  98.                 ComParam................................15
  99.  
  100.                 ComBaud.................................16
  101.  
  102.                 ComMode.................................17
  103.  
  104.                 ComHandshake............................19
  105.  
  106.                 ComTx...................................20
  107.  
  108.         
  109.         Input Functions
  110.  
  111.                 Description.............................22
  112.  
  113.                 ComGetc.................................23
  114.  
  115.                 ComGets.................................24 
  116.  
  117.                 ComIn...................................25
  118.  
  119.                 ComFlushRx..............................26
  120.  
  121.  
  122.  
  123.  
  124.                                        2 
  125.  
  126.  
  127.  
  128.                 ComLenRx................................27
  129.                 
  130.                 ComPeek.................................28
  131.  
  132.  
  133.         Output Functions
  134.  
  135.                 Description.............................30
  136.  
  137.                 ComPutc.................................31
  138.  
  139.                 ComPuts.................................32
  140.  
  141.                 ComOut..................................33
  142.  
  143.                 ComFlushTx..............................34
  144.  
  145.                 ComLenTx................................35
  146.  
  147.                 
  148.         Status Functions
  149.  
  150.                 Description.............................36
  151.  
  152.                 ComError................................37
  153.  
  154.                 ComStatus...............................38
  155.  
  156.                 ComRts..................................39
  157.  
  158.                 ComDtr..................................40
  159.  
  160.                 ComOut1.................................41
  161.  
  162.         
  163.         Data Integrity
  164.  
  165.                 Description.............................43
  166.  
  167.                 ComChecksum.............................44
  168.  
  169.                 ComCrc16................................45
  170.          
  171.  
  172.         Miscellaneous Functions
  173.  
  174.                 Description.............................47
  175.  
  176.                 ComSetBreak.............................48
  177.  
  178.                 ComClrBreak.............................48
  179.  
  180.                 ComPutScrtch............................49
  181.  
  182.                 ComGetScrtch............................50
  183.  
  184.  
  185.  
  186.                                        3 
  187.  
  188.  
  189.  
  190.         Appendix A - Things to Look Out For.............52
  191.  
  192.         Appendix B - Handshaking Schemes................53
  193.         
  194.         Appendix C - The UART Chip......................55
  195.  
  196.         Appendix D - The RS-232 Standard ?!!............56
  197.  
  198.         Appendix E - Registration Form..................57
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.                                        4 
  249.  
  250.  
  251.  
  252.         Acknowledgments.
  253.  
  254.         I would like to thank Earl Jensen and Steve Hill for technical 
  255.         review, Diana Hill for proofreading the documentation, Chip Curry 
  256.         and Luxtron Corp for beta testing, John Kerwin for technical advice,
  257.         and Ton Kneist and Noël Gaskell for facilitating development.
  258.  
  259.  
  260.         General Description.
  261.  
  262.         Cport is a C language interrupt-driven serial communication library 
  263.         for IBM and compatible computers. Cport is compatible with most if
  264.         not all DOS based C compilers.
  265.         
  266.  
  267.         Some of Cport's features are:
  268.  
  269.         ∙ Supports COM 1 to COM 4.
  270.         ∙ Baud rates from 50 to 115200 baud.
  271.         ∙ Built in hardware and software handshaking.
  272.         ∙ Both transmitter and receiver are interrupt-driven.
  273.         ∙ Adjustable transmit and receive queues.
  274.         ∙ Queues as large as 65534 bytes each.
  275.         ∙ Transmit and receive queues are handled internally.
  276.         ∙ Written in assembly language for optimum speed and efficiency.
  277.  
  278.  
  279.  
  280.         DISCLAIMER.
  281.                
  282.         Cport is provided AS IS. Bri Productions specifically disclaims 
  283.         any and all warranties, expressed or implied, including fitness 
  284.         for a particular purpose. Use this product at your own risk. 
  285.  
  286.  
  287.  
  288.         Registration.
  289.  
  290.         Cport is a user supported software product. Distribution of 
  291.         this product, unaltered and without charge, is encouraged. If 
  292.         you find this product useful, you are encouraged to register
  293.         your copy. This allows Bri Productions to continue to provide 
  294.         support low cost, high quality shareware.
  295.  
  296.         The registration fee is $25.00 US dollars and includes libraries 
  297.         for small, medium, compact and large memory models and complete 
  298.         source code. A registration form is provided in Appendix E.
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.                                        5 
  311.  
  312.  
  313.  
  314.         Contacting the author.
  315.  
  316.         Bri Productions may be contacted by any of the following means:
  317.  
  318.         CompuServe      -       76635,2246
  319.         U.S. mail       -       Bri Productions
  320.                                 P.O.Box 7121
  321.                                 Fremont, CA 94537-7121
  322.  
  323.         CompuServe is a trademark of CompuServe Inc. 
  324.  
  325.  
  326.         A typedef.
  327.  
  328.         You will see the data type 'byte' throughout the Cport library.
  329.         This is a typedef defined in cport.h. Byte is an unsigned 
  330.         character used for byte size values.
  331.  
  332.  
  333.  
  334.         "Remote device"
  335.  
  336.         In this documentation, the expression "remote device" is used 
  337.         to describe the device that the computer is communicating with. 
  338.         The actual device may be a modem, serial printer or other 
  339.         communications device.
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.                                        6 
  373.  
  374.  
  375.  
  376.         Control Functions
  377.  
  378.         Control functions manage the parameters and the modes in
  379.         which the serial port will operate. There are two functions 
  380.         which are mandatory: ComOpen or ComOpenS, and ComClose.
  381.         
  382.         ComOpen and/or ComOpenS open a serial port and sets the 
  383.         parameters and modes. ComClose closes the opened serial port. 
  384.         Failing to call ComClose prior to a program's termination will 
  385.         cause the operating system crash.
  386.  
  387.         ComBaud and ComMode modify one or more of the parameters/modes 
  388.         of the serial port. ComParam reports on the current parameters
  389.         and handshaking under which the currently opened serial port
  390.         are operating.
  391.  
  392.         ComHandshake determines if and what type of handshaking
  393.         scheme will be enabled and sets the receiver threshold. ComTx 
  394.         turns the transmitter on and off, allowing you to implement 
  395.         your own handshaking scheme. See Appendix B for a description 
  396.         of handshaking.
  397.  
  398.         
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.                                        7 
  435.  
  436.  
  437.  
  438.         ----------------------------------------------------------------
  439.         ComOpen
  440.         ----------------------------------------------------------------
  441.  
  442.         Function
  443.         
  444.                 Opens or reopens a serial port.
  445.  
  446.  
  447.         Syntax  
  448.  
  449.                 #include "cport.h"
  450.                 int ComOpen(unsigned com, int baud, byte mode, 
  451.                             unsigned rxQ, unsigned txQ);
  452.  
  453.  
  454.         Parameters
  455.  
  456.                 com   - Constant that defines the serial port to be 
  457.                         opened. Possibilities are:
  458.  
  459.                                 COM1  (0xC3F8)
  460.                                 COM2  (0xB2F8)
  461.                                 COM3  (0xC3E8)
  462.                                 COM4  (0xB2E8)
  463.  
  464.                 baud  - Constant defining the baud rate to which the 
  465.                         serial port will be initialized. Possibilities 
  466.                         are:
  467.  
  468.                                 B115200    (1)
  469.                                 B57600     (2)  
  470.                                 B38400     (3)
  471.                                 B19200     (6)           
  472.                                 B9600     (12)           
  473.                                 B7200     (16)           
  474.                                 B4800     (24)       
  475.                                 B3600     (32)
  476.                                 B2400     (48)
  477.                                 B2000     (58)
  478.                                 B1800     (64)
  479.                                 B1200     (96)
  480.                                 B600     (192)
  481.                                 B300     (384)
  482.                                 B150     (768)
  483.                                 B110    (1047)
  484.                                 B75     (1536)
  485.                                 B50     (2304)
  486.  
  487.  
  488.                 mode  - Bit ORed constant defining the word 
  489.                         length, number of stop bits and the 
  490.                         parity that the serial port will use. 
  491.                         Possibilities are:
  492.  
  493.  
  494.  
  495.  
  496.                                        8 
  497.  
  498.  
  499.  
  500.                                 W8  (0x03)  -  8 bit words     
  501.                                 W7  (0x02)  -  7 bit words    
  502.                                 W6  (0x01)  -  6 bit words    
  503.                                 W5  (0x00)  -  5 bit words       
  504.  
  505.                                 S1  (0x00)  -  1 stop bit         
  506.                                 S2  (0x04)  -  2 stop bits
  507.  
  508.                                 NONE   (0x00) -  No parity
  509.                                 ODD    (0x08) -  Odd parity
  510.                                 EVEN   (0x18) -  Even parity
  511.                                 MARK   (0x28) -  Mark parity
  512.                                 SPACE  (0x38) -  Space parity
  513.  
  514.  
  515.                 rxQ  - Size, in bytes, of the receive queue.
  516.                 
  517.                 txQ   - Size in bytes, of the transmit queue.
  518.  
  519.                       * Limited to 32K in small and medium
  520.                         memory models.
  521.  
  522.  
  523.         Remarks
  524.  
  525.                 ComOpen opens the serial port 'com' for communications.
  526.                 If a serial port has been previously opened, it will
  527.                 be close prior to the new serial port being opened.
  528.                 The new serial port can be a different port or the
  529.                 same port with new parameters. This feature allows
  530.                 the programmer to reopen serial ports without first
  531.                 closing the previous one.
  532.  
  533.                 The 4 most significant bits in 'com' represent the 
  534.                 interrupt vector, while the lower 12 bits represent 
  535.                 the port address. Constants are provided in cport.h
  536.                 which will be satisfactory in most cases.
  537.  
  538.                 The parameter 'baud' is the baud rate divisor which 
  539.                 determines the baud rate. Constants are provided in
  540.                 cport.h for the most common baud rates. The 'baud' 
  541.                 rate divisor is defined as f / (16 * baud rate) where
  542.                 f is 1.8432e+6. 
  543.                 
  544.                 Achieving baud rates above 19200 is largely dependent
  545.                 on many variables such as machine speed, cable length,
  546.                 handshaking activity, degree bidirectional transfers,
  547.                 the type UART (see Appendix C) used and activity of 
  548.                 other hardware interrupts. 
  549.                 
  550.                 The parameter 'mode' determines the word length,
  551.                 number of stop bits and parity of the exchanged data.
  552.                 Bits 0 & 1 determine the word length, bit 2
  553.                 determines the number of stop bits, bit 3 enables
  554.                 parity and bits 4 & 5 determine the type of parity
  555.  
  556.  
  557.  
  558.                                        9 
  559.  
  560.  
  561.  
  562.                 check. Constants which can be ORed together are
  563.                 provided in cport.h.
  564.                 
  565.                 'RxQ' and 'txQ' set the size of the receive and transmit
  566.                 queues respectively. 'RxQ' should be at least 4 times
  567.                 larger than the largest string or block of data the
  568.                 programmer intends to fetch from the queue at once. '
  569.                 TxQ' can be set as small as 1, but is not recommended. 
  570.                 In the small and medium memory models, where all data 
  571.                 is limited to 64K, the size queues are limited. In
  572.                 the compact and large memory models, each queue can
  573.                 be as large as 64K -1 (65534 bytes). 
  574.  
  575.  
  576.         Return value
  577.         
  578.                 If successful, ComOpen returns 0. In the event of an 
  579.                 error, it returns one of the following error codes:
  580.  
  581.                         BAD_COM   (2) -  Bad 'com' parameter.
  582.                         NO_UART   (3) -  No UART chip detected.               
  583.                         RCV_ALC   (4) -  Receive queue allocation error.
  584.                         TX_ALC    (5) -  Transmit queue allocation error.
  585.  
  586.  
  587.         See also
  588.  
  589.                 ComOpenS ComClose ComBaud ComMode
  590.  
  591.  
  592.         Example 
  593.        
  594.                 Open COM1 at 9600 baud with 8 bit words, 1 stop bit,
  595.                 no parity, a 1024 byte receiver and a 512 byte
  596.                 transmitter.
  597.                 
  598.                 {
  599.                 int rv;
  600.                            
  601.                    rv = ComOpen(COM1, B9600, W8|S1|NONE, 1024, 512);
  602.  
  603.                    if(rv)
  604.                    {
  605.  
  606.                       ...error handler 
  607.  
  608.                    }
  609.  
  610.                    ...more code
  611.                 }
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.                                        10
  621.  
  622.  
  623.  
  624.         ----------------------------------------------------------------
  625.         ComOpenS
  626.         ----------------------------------------------------------------
  627.  
  628.         Function
  629.         
  630.                 Opens or reopens a serial port.
  631.  
  632.  
  633.         Syntax  
  634.  
  635.                 #include "cport.h"
  636.                 int ComOpenS(const C_param *param);
  637.  
  638.  
  639.         Parameters
  640.  
  641.                 param   - Pointer to the structure of type 'C_param'.
  642.  
  643.                         The structure 'C_param' is defined as follows:
  644.  
  645.                         struct C_param{               
  646.                            unsigned com;              
  647.                            int      baud;             
  648.                            byte     mode;             
  649.                            unsigned rxQ;              
  650.                            unsigned txQ;              
  651.                            byte     htype;            
  652.                            unsigned thresh;           
  653.                            };
  654.  
  655.                 com   - Constant that defines the serial port to be 
  656.                         opened. Possibilities are:
  657.  
  658.                                 COM1  (0xC3F8)
  659.                                 COM2  (0xB2F8)
  660.                                 COM3  (0xC3E8)
  661.                                 COM4  (0xB2E8)
  662.  
  663.                 baud  - Constant defining the baud rate to which
  664.                         the serial port will be initialized.
  665.                         Possibilities are:
  666.  
  667.                                 B115200    (1)
  668.                                 B57600     (2)  
  669.                                 B38400     (3)
  670.                                 B19200     (6)           
  671.                                 B9600     (12)           
  672.                                 B7200     (16)           
  673.                                 B4800     (24)       
  674.                                 B3600     (32)
  675.                                 B2400     (48)
  676.                                 B2000     (58)
  677.                                 B1800     (64)
  678.                                 B1200     (96)
  679.  
  680.  
  681.  
  682.                                        11
  683.  
  684.  
  685.  
  686.                                 B600     (192)
  687.                                 B300     (384)
  688.                                 B150     (768)
  689.                                 B110    (1047)
  690.                                 B75     (1536)
  691.                                 B50     (2304)
  692.  
  693.  
  694.                 mode  - Bit ORed constant defining the word 
  695.                         length, number of stop bits and the 
  696.                         parity that the serial port will use. 
  697.                         Possibilities are:
  698.  
  699.                                 W8  (0x03)  -  8 bit words     
  700.                                 W7  (0x02)  -  7 bit words    
  701.                                 W6  (0x01)  -  6 bit words    
  702.                                 W5  (0x00)  -  5 bit words       
  703.         
  704.                                 S1  (0x00)  -  1 stop bit         
  705.                                 S2  (0x04)  -  2 stop bits
  706.         
  707.                                 NONE   (0x00) -  No parity)
  708.                                 ODD    (0x08) -  Odd parity)
  709.                                 EVEN   (0x18) -  Even parity)
  710.                                 MARK   (0x28) -  Mark parity)
  711.                                 SPACE  (0x38) -  Space parity)
  712.  
  713.  
  714.                 rxQ  - Size, in bytes, of the receive queue.
  715.  
  716.                 txQ   - Size in bytes, of the transmit queue.
  717.  
  718.                       * Limited to 32K in small and medium
  719.                         memory models.
  720.  
  721.  
  722.                 htype - Type of handshaking to be used. The two types
  723.                        are:
  724.  
  725.                        SOFT  (1) - Uses XON and XOFF characters.
  726.                        HARD  (2) - Uses RTS and CTS.
  727.  
  728.  
  729.                 thresh - Receive queue threshold.
  730.  
  731.  
  732.         Remarks 
  733.                 
  734.                 ComOpenS is an alternative to ComOpen for opening or 
  735.                 a reopening serial port. It takes a pointer to a 
  736.                 structure of parameters instead of taking discrete 
  737.                 arguments. It also can set the handshaking without 
  738.                 making a separate call to ComHandshake.
  739.  
  740.                 The 4 most significant bits in 'com' represent the 
  741.  
  742.  
  743.  
  744.                                        12
  745.  
  746.  
  747.  
  748.                 interrupt vector, while the lower 12 bits represent 
  749.                 the port address. Constants are provided in cport.h
  750.                 which will be satisfactory in most cases.
  751.  
  752.                 The parameter 'baud' is the baud rate divisor which 
  753.                 determines the baud rate. Constants are provided in
  754.                 cport.h for the most common baud rates. The 'baud' 
  755.                 rate divisor is defined as f / (16 * baud rate) where
  756.                 f is 1.8432e+6. 
  757.                 
  758.                 Achieving baud rates above 19200 is largely dependent
  759.                 on many variables such as machine speed, cable length,
  760.                 handshaking activity, degree bidirectional transfers,
  761.                 the type UART (see Appendix C) used and activity of 
  762.                 other hardware interrupts. 
  763.                 
  764.                 The parameter 'mode' determines the word length,
  765.                 number of stop bits and parity of the exchanged data.
  766.                 Bits 0 & 1 determine the word length, bit 2
  767.                 determines the number of stop bits, bit 3 enables
  768.                 parity and bits 4 & 5 determine the type of parity
  769.                 check. Constants which can be ORed together are
  770.                 provided in cport.h.
  771.                 
  772.                 'RxQ' and 'txQ' set the size of the receive and transmit
  773.                 queues respectively. 'RxQ' should be at least 4 times
  774.                 larger than the largest string or block of data the
  775.                 programmer intends to fetch from the queue at once. '
  776.                 TxQ' can be set as small as 1, but is not recommended. 
  777.                 In the small and medium memory models, where all data 
  778.                 is limited to 64K, the size queues are limited. In
  779.                 the compact and large memory models, each queue can
  780.                 be as large as 64K -1 (65534). 
  781.  
  782.                 The parameter 'htype' defines the type of flow
  783.                 control (handshaking) to be used by the transmitter
  784.                 and receiver. 'Threshold' determines when the
  785.                 receiver will assert of disassert the selected
  786.                 handshaking.
  787.  
  788.  
  789.         Return value
  790.         
  791.                 If successful, ComOpenS returns 0. In the event of an 
  792.                 error, it returns one of the following error codes:
  793.  
  794.                         BAD_COM   (2) -  Bad 'com' parameter.
  795.                         NO_UART   (3) -  No UART chip detected.               
  796.                         RCV_ALC   (4) -  Receive queue allocation error.
  797.                         TX_ALC    (5) -  Transmit queue allocation error.
  798.  
  799.  
  800.         See also
  801.  
  802.                 ComOpen ComParam ComHandshake ComClose  
  803.  
  804.  
  805.  
  806.                                        13
  807.  
  808.  
  809.  
  810.         Example 
  811.  
  812.                 Switch to COM2 with the same parameters.
  813.  
  814.                 {
  815.                 struct C_param param;
  816.  
  817.                    ComParam(¶m);
  818.                    param.com = COM2;
  819.                         
  820.                    ComOpenS(¶m);
  821.                 }
  822.  
  823.  
  824.         ----------------------------------------------------------------
  825.         ComReopen
  826.         ----------------------------------------------------------------
  827.  
  828.                 This function has become obsolete, however, the
  829.                 function name 'ComReopen' has been retained as a
  830.                 function macro to maintain backwards compatibility.
  831.  
  832.                 NOTE: See ComOpen and ComOpenS
  833.  
  834.  
  835.         ----------------------------------------------------------------
  836.         ComClose
  837.         ----------------------------------------------------------------
  838.  
  839.         Function        
  840.         
  841.                 Closes the currently opened serial port.
  842.  
  843.  
  844.         Syntax  
  845.  
  846.                 #include "cport.h"
  847.                 void ComClose(void);
  848.  
  849.         Remarks 
  850.  
  851.                 ComClose closes the currently opened serial port. 
  852.                 Failing to call ComClose before a program is terminated 
  853.                 will cause the operating system crash. It need only be 
  854.                 called once prior to exiting, regardless of how many 
  855.                 times serial ports have been reopened.
  856.  
  857.  
  858.         See also
  859.  
  860.                 ComOpen ComOpenS
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.                                        14
  869.  
  870.  
  871.  
  872.         Example
  873.  
  874.                 Close the serial port prior to terminating.
  875.  
  876.                 {
  877.  
  878.                   ...closing code
  879.  
  880.                   ComClose();
  881.                   exit(0)
  882.                 }
  883.  
  884.  
  885.  
  886.  
  887.         ----------------------------------------------------------------
  888.         ComParam
  889.         ----------------------------------------------------------------
  890.  
  891.         Function        
  892.         
  893.                 Retrieves the parameters of the currently opened serial 
  894.                 port.
  895.  
  896.  
  897.         Syntax  
  898.  
  899.                 #include "cport.h"
  900.                 void ComParam(const struct C_param *param);
  901.  
  902.         Remarks 
  903.  
  904.                 ComParam retrieves the parameters and handshaking
  905.                 status of the currently opened serial port and stores 
  906.                 them in a structure of type 'C_param'. The structure 
  907.                 'C_param' is defined as follows:
  908.  
  909.                         struct C_param{               
  910.                            unsigned com;              
  911.                            int      baud;             
  912.                            byte     mode;             
  913.                            unsigned rxQ;              
  914.                            unsigned txQ;              
  915.                            byte     htype;            
  916.                            unsigned thresh;           
  917.                            };
  918.  
  919.                 See ComOpenS for a discussion of the individual
  920.                 elements.
  921.  
  922.         See also
  923.  
  924.                 ComOpen ComOpenS ComMode ComBaud
  925.  
  926.  
  927.  
  928.  
  929.  
  930.                                        15
  931.  
  932.  
  933.  
  934.         Example
  935.  
  936.                 Switch to COM2 with the same parameters.
  937.  
  938.                 {
  939.                 struct C_param param;
  940.  
  941.                    ComParam(¶m);
  942.                    param.com = COM2;
  943.                         
  944.                    ComOpenS(¶m);
  945.                 }
  946.  
  947.  
  948.  
  949.         ----------------------------------------------------------------
  950.         ComBaud
  951.         ----------------------------------------------------------------
  952.  
  953.         Function        
  954.         
  955.                Changes the baud rate of the currently opened serial port.
  956.  
  957.  
  958.         Syntax
  959.  
  960.                 #include "cport.h"
  961.                 void ComBaud(int baud);
  962.  
  963.  
  964.         Parameters
  965.  
  966.                 baud  - Constant defining the baud rate to which
  967.                         the serial port will be initialized.
  968.                         Possibilities are:
  969.  
  970.                                 B115200    (1)
  971.                                 B57600     (2)  
  972.                                 B38400     (3)
  973.                                 B19200     (6)           
  974.                                 B9600     (12)           
  975.                                 B7200     (16)           
  976.                                 B4800     (24)       
  977.                                 B3600     (32)
  978.                                 B2400     (48)
  979.                                 B2000     (58)
  980.                                 B1800     (64)
  981.                                 B1200     (96)
  982.                                 B600     (192)
  983.                                 B300     (384)
  984.                                 B150     (768)
  985.                                 B110    (1047)
  986.                                 B75     (1536)
  987.                                 B50     (2304)
  988.  
  989.  
  990.  
  991.  
  992.                                        16
  993.  
  994.  
  995.  
  996.         Remarks
  997.                         
  998.                 The parameter 'baud' is the baud rate divisor which 
  999.                 determines the baud rate. Constants are provided in
  1000.                 cport.h for the most common baud rates. The 'baud' 
  1001.                 rate divisor is defined as f / (16 * baud rate) where
  1002.                 f is 1.8432e+6. 
  1003.  
  1004.                 Achieving baud rates above 19200 are largely dependent
  1005.                 on many variables such as machine speed, cable length,
  1006.                 handshaking activity, degree bidirectional transfers,
  1007.                 the type of UART used and activity of other hardware 
  1008.                 interrupts. 
  1009.                
  1010.  
  1011.         See also
  1012.  
  1013.                 ComOpen ComOpenS ComParam ComMode
  1014.  
  1015.  
  1016.         Example
  1017.  
  1018.                 Set the currently opened serial port to the next baud 
  1019.                 rate in the table 'baud'.
  1020.  
  1021.                 {
  1022.                 static int indx = 0;
  1023.                 int baud[4] = { B9600,
  1024.                                 B2400,
  1025.                                 B1200,
  1026.                                 B300
  1027.                                 };
  1028.  
  1029.                    indx++;
  1030.                    indx %= 4;
  1031.                 
  1032.                    ComBaud(baud[indx]);
  1033.                 }
  1034.                 
  1035.  
  1036.  
  1037.         ----------------------------------------------------------------
  1038.         ComMode
  1039.         ----------------------------------------------------------------
  1040.  
  1041.         Function        
  1042.         
  1043.                Changes the word length, number of stop bits and parity 
  1044.                of the opened serial port.
  1045.  
  1046.  
  1047.         Syntax
  1048.  
  1049.                 #include "cport.h"
  1050.                 void ComMode(int mode);
  1051.  
  1052.  
  1053.  
  1054.                                        17
  1055.  
  1056.  
  1057.  
  1058.         Parameters
  1059.  
  1060.                 mode  - Bit ORed constant defining the word length, 
  1061.                         number of stop bits and the parity that the 
  1062.                         serial port will use. Possibilities are:
  1063.  
  1064.                                 W8  (0x03)  -  8 bit words     
  1065.                                 W7  (0x02)  -  7 bit words    
  1066.                                 W6  (0x01)  -  6 bit words    
  1067.                                 W5  (0x00)  -  5 bit words       
  1068.         
  1069.                                 S1  (0x00)  -  1 stop bit         
  1070.                                 S2  (0x04)  -  2 stop bits
  1071.         
  1072.                                 NONE   (0x00) -  No parity)
  1073.                                 ODD    (0x08) -  Odd parity)
  1074.                                 EVEN   (0x18) -  Even parity)
  1075.                                 MARK   (0x28) -  Mark parity)
  1076.                                 SPACE  (0x38) -  Space parity)
  1077.  
  1078.  
  1079.         Remarks
  1080.  
  1081.                 The parameter 'mode' determines the word length,
  1082.                 number of stop bits and parity of the exchanged data.
  1083.                 Bits 0 & 1 determine the word length, bit 2
  1084.                 determines the number of stop bits, bit 3 enables
  1085.                 parity and bits 4 & 5 determine the type of parity
  1086.                 check. Constants that can be ORed together are
  1087.                 provided in cport.h.
  1088.                 
  1089.  
  1090.         See also
  1091.  
  1092.                 ComOpen ComOpenS ComParam ComBaud
  1093.  
  1094.  
  1095.         Example
  1096.  
  1097.                 Toggle the currently opened serial port to the next mode
  1098.                 in the table 'mode'.
  1099.  
  1100.                 {
  1101.                 static int indx = 0;
  1102.                 int mode[2] = { W8|S1|NONE,
  1103.                                 W7|S1|EVEN,
  1104.                                 };
  1105.  
  1106.                    indx ^= 1;
  1107.                 
  1108.                    ComMode(mode[indx]);
  1109.                 }
  1110.                 
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.                                        18
  1117.  
  1118.  
  1119.  
  1120.         ----------------------------------------------------------------
  1121.         ComHandshake
  1122.         ----------------------------------------------------------------
  1123.  
  1124.         Function        
  1125.         
  1126.                 Enables or disables flow control (handshaking).
  1127.  
  1128.  
  1129.         Syntax
  1130.  
  1131.                 #include "cport.h"
  1132.                 void ComHandshake(byte htype, unsigned thresh);
  1133.  
  1134.  
  1135.         Parameters
  1136.         
  1137.                 type - Type of handshaking to be used. The two types
  1138.                        are:
  1139.  
  1140.                                SOFT  (1) - Uses XON and XOFF characters.
  1141.                                HARD  (2) - Uses RTS and CTS.
  1142.  
  1143.  
  1144.                 thresh - Receive queue threshold.
  1145.  
  1146.  
  1147.         Remarks
  1148.  
  1149.                 ComHandshake enables or disables software and/or
  1150.                 hardware handshaking. In software handshaking, XON 
  1151.                 and XOFF characters are transmitted and received to 
  1152.                 suspend and resume communications. In hardware 
  1153.                 handshaking uses the RTS and CTS lines to suspend 
  1154.                 and resume communications. 'Thresh' determines when
  1155.                 the receiver will assert and disassert.
  1156.                 
  1157.  
  1158.         See also 
  1159.  
  1160.                 ComParam ComOpenS ComTx
  1161.  
  1162.                 
  1163.         Example 
  1164.         
  1165.                 If COM1 is successfully opened, turn on software
  1166.                 handshaking and set the receiver threshold to 3/4 of
  1167.                 the receiver's full size.
  1168.  
  1169.                 {
  1170.                 int rv;
  1171.                            
  1172.                    rv = ComOpen(COM1, B9600, W8|S1|NONE, 1024, 512);
  1173.                    if(rv)
  1174.                    {
  1175.  
  1176.  
  1177.  
  1178.                                        19
  1179.  
  1180.  
  1181.  
  1182.                       ...error handler 
  1183.                    }
  1184.  
  1185.                    ComHandshake(SOFT, RCVQ_SIZE * 3 / 4);
  1186.  
  1187.                    ...more code
  1188.                 }
  1189.  
  1190.  
  1191.  
  1192.         ----------------------------------------------------------------
  1193.         ComTx
  1194.         ----------------------------------------------------------------
  1195.         
  1196.         Function
  1197.                 
  1198.                 Turns the transmitter on or off
  1199.  
  1200.  
  1201.         Syntax
  1202.                 
  1203.                 #include "cport.h"
  1204.                 void ComTx(byte on_off);
  1205.  
  1206.  
  1207.         Parameters
  1208.  
  1209.                 on_off - Determines whether the transmitter is to be
  1210.                          turned on or off. Constants are provided in
  1211.                          cport.h. Possibilities are:
  1212.  
  1213.                                  ON  (1) - Turns the transmitter on
  1214.                                  OFF (0) - Turns the transmitter off
  1215.  
  1216.  
  1217.         Remarks
  1218.  
  1219.                 ComTx allows you to turn the transmitter on and off
  1220.                 to suspend and resume transmissions. This function
  1221.                 can be very useful when implementing your own
  1222.                 handshaking scheme.
  1223.  
  1224.  
  1225.         See also
  1226.  
  1227.                 ComHandshake ComOpenS ComParam 
  1228.  
  1229.  
  1230.         Example
  1231.  
  1232.                 If the transmitter is off ('off') and needs to be
  1233.                 turned on ('turn_on'), turn it on. If the transmitter 
  1234.                 is on and needs to be turned off, turn it off.
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.                                        20
  1241.  
  1242.  
  1243.  
  1244.                 {
  1245.  
  1246.                    if(off && turn_on)
  1247.                       ComTx(ON);
  1248.  
  1249.                    else if(!off && turn_off)
  1250.                       ComTx(OFF);
  1251.  
  1252.                 }
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260.  
  1261.  
  1262.  
  1263.  
  1264.  
  1265.  
  1266.  
  1267.  
  1268.  
  1269.  
  1270.  
  1271.  
  1272.  
  1273.  
  1274.  
  1275.  
  1276.  
  1277.  
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289.  
  1290.  
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.                                        21
  1303.  
  1304.  
  1305.  
  1306.         Input Functions
  1307.  
  1308.         Input fuctions interface with the receiver and the internal
  1309.         receive queue. 
  1310.  
  1311.         There are four functions for fetching from the receive
  1312.         queue. ComGetc and ComGets can be used to fetch control codes
  1313.         as well as normal ascii characters, while ComIn is intended
  1314.         for fetching binary data. ComPeek returns a copy of the head
  1315.         character without removing it from the queue.
  1316.  
  1317.         ComFlushRx flushes the receiver while ComLenRx finds
  1318.         the number of characters in the queue.
  1319.         
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326.  
  1327.  
  1328.  
  1329.  
  1330.  
  1331.  
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.                                        22
  1365.  
  1366.  
  1367.  
  1368.         ----------------------------------------------------------------
  1369.         ComGetc
  1370.         ----------------------------------------------------------------
  1371.  
  1372.         Function       
  1373.                 
  1374.                 Fetches a character from the receive queue.
  1375.  
  1376.  
  1377.         Syntax    
  1378.         
  1379.                 #include "cport.h"
  1380.                 char ComGetc(void);
  1381.  
  1382.                        
  1383.         Remarks        
  1384.  
  1385.                 ComGetc fetches the head character from the receive
  1386.                 queue. If there are no characters in the receive
  1387.                 queue, ComGetc returns a null character ('\0').
  1388.                 
  1389.  
  1390.         Return value   
  1391.         
  1392.                 On Success, ComGetc returns the character from the 
  1393.                 receive queue. If it fails it returns a '\0'.
  1394.  
  1395.  
  1396.         See also
  1397.  
  1398.                 ComPutc ComGets ComIn ComPeek        
  1399.  
  1400.  
  1401.         Example 
  1402.  
  1403.                 Get a character from the serial port and test if it
  1404.                 is a start of header control character.
  1405.  
  1406.                 #define SOH 1
  1407.                 
  1408.                 {
  1409.                 char c;
  1410.  
  1411.                    c = ComGetc();
  1412.  
  1413.                    if(c == SOH)      
  1414.                    {
  1415.  
  1416.                       ...SOH code
  1417.                    
  1418.                    }
  1419.                    else
  1420.                       return c;
  1421.                 }
  1422.        
  1423.  
  1424.  
  1425.  
  1426.                                        23
  1427.  
  1428.  
  1429.  
  1430.         ----------------------------------------------------------------
  1431.         ComGets
  1432.         ----------------------------------------------------------------
  1433.  
  1434.         Function        
  1435.         
  1436.                 Fetches a string of characters from the receive queue.
  1437.  
  1438.  
  1439.         Syntax  
  1440.                 
  1441.                 #include "cport.h"
  1442.                 char *ComGets(char *str, int max_c, char term_c);     
  1443.  
  1444.  
  1445.         Parameters
  1446.                 
  1447.                 *str  - Pointer to the buffer where the string of 
  1448.                         characters will be stored. 
  1449.  
  1450.                 max_c - Maximum number of characters to fetch. 
  1451.  
  1452.                 term_c - Termination character. ComGets will return
  1453.                         if this character is encountered.
  1454.  
  1455.  
  1456.         Remarks
  1457.  
  1458.                 ComGets fetches a string of characters from the
  1459.                 receive queue. The string will be stored in 'str'.
  1460.                 'str' must be at least 'max_c' + 1 long to allow 
  1461.                 for the null terminator.
  1462.  
  1463.                 ComGets will return on the first occurrence of one of
  1464.                 three conditions: 1) the character 'term_c' is 
  1465.                 encountered; 2) 'max_c' characters are reached; or 3) 
  1466.                 No more characters are available in the receive queue.
  1467.  
  1468.  
  1469.         Return value
  1470.  
  1471.                 ComGets returns a pointer to the null terminated
  1472.                 string.
  1473.  
  1474.  
  1475.         See also
  1476.  
  1477.                 ComPuts ComGetc ComIn ComLenRx
  1478.  
  1479.  
  1480.         Example
  1481.                 
  1482.                 Get a line of max 80 characters from the receive queue
  1483.                 and store it in 'str'. The string will be echoed to the 
  1484.                 display.
  1485.  
  1486.  
  1487.  
  1488.                                        24
  1489.  
  1490.  
  1491.  
  1492.  
  1493.                 {
  1494.                 char str[81];
  1495.  
  1496.                    if(ComLenRx() > 80)
  1497.                       puts(ComGets(str, 80, '\n'));
  1498.                 }
  1499.                    
  1500.  
  1501.         ----------------------------------------------------------------
  1502.         ComIn
  1503.         ----------------------------------------------------------------
  1504.  
  1505.         Function
  1506.  
  1507.                 Fetches a block of bytes from the receive queue.
  1508.  
  1509.  
  1510.         Syntax
  1511.  
  1512.                 #include "cport.h"
  1513.                 unsigned ComIn(void *abyte, unsigned num_byte);
  1514.  
  1515.  
  1516.         Parameters  
  1517.         
  1518.                 *abyte - Pointer to the buffer where the block of bytes 
  1519.                         will be stored.
  1520.  
  1521.                 num_byte - Number of bytes to get from the receive queue.
  1522.  
  1523.  
  1524.         Remarks
  1525.  
  1526.                 ComIn fetches a block of bytes from the receive queue 
  1527.                 and stores it in 'abyte'. ComIn will always fetch
  1528.                 'num_byte' bytes unless not enough bytes are available.
  1529.                 ComIn is well suited for receiving binary data.
  1530.  
  1531.  
  1532.         Return value
  1533.  
  1534.                 ComIn returns the number of bytes fetched. 
  1535.  
  1536.  
  1537.         See also
  1538.                 
  1539.                 ComOut ComGetc ComGets ComLenRx
  1540.  
  1541.  
  1542.         Example
  1543.  
  1544.                 Get a block of 1024 bytes from the serial port.
  1545.  
  1546.  
  1547.  
  1548.  
  1549.  
  1550.                                        25
  1551.  
  1552.  
  1553.  
  1554.                 #define BLK_SZ 1024
  1555.                 {
  1556.                 byte block[BLK_SZ];
  1557.                 unsigned rs;
  1558.  
  1559.                    if(ComLenRx() > BLK_SZ)
  1560.                    {
  1561.                       rs = ComIn(block, BLK_SZ);  
  1562.                       if(rs < BLK_SZ)
  1563.                       {
  1564.                          ...error handler
  1565.                       }
  1566.                    }
  1567.                 }
  1568.  
  1569.  
  1570.  
  1571.         ----------------------------------------------------------------
  1572.         ComFlushRx
  1573.         ----------------------------------------------------------------
  1574.  
  1575.         Function
  1576.  
  1577.                 Flushes the receiver and receive queue.
  1578.  
  1579.         Syntax
  1580.  
  1581.                 #include "cport.h"
  1582.                 void ComFlushRx(void);
  1583.  
  1584.  
  1585.         Remarks
  1586.  
  1587.                 ComFlushRx flushes the receive queue and the UART of
  1588.                 any existing characters.
  1589.  
  1590.  
  1591.         See also
  1592.  
  1593.                 ComLenRx ComFlushTx
  1594.  
  1595.  
  1596.         Example
  1597.  
  1598.                 If a receive flush command is in order, flush the
  1599.                 receiver.
  1600.  
  1601.                 #define ...
  1602.                 #define RCV_FLSH 2
  1603.                 #define ...
  1604.                 {
  1605.                 int command;
  1606.                  
  1607.        
  1608.                    ...fetch command     
  1609.  
  1610.  
  1611.  
  1612.                                        26
  1613.  
  1614.  
  1615.  
  1616.  
  1617.                    switch(command)
  1618.                    {
  1619.  
  1620.                         case ...:
  1621.                            ...case code
  1622.                            break;
  1623.  
  1624.                         case RCV_FLSH:
  1625.                            ComFlushRx();
  1626.                            break;
  1627.  
  1628.                         case ...:
  1629.                            ...case code
  1630.                            break;
  1631.  
  1632.                    }
  1633.                  }
  1634.  
  1635.  
  1636.  
  1637.         ----------------------------------------------------------------
  1638.         ComLenRx
  1639.         ----------------------------------------------------------------
  1640.  
  1641.         Function
  1642.  
  1643.                 Calculates the number of characters in the receive queue.
  1644.  
  1645.  
  1646.         Syntax
  1647.  
  1648.                 #include "cport.h"
  1649.                 unsigned ComLenRx(void);
  1650.  
  1651.  
  1652.         Remarks
  1653.  
  1654.                 ComLenRx calculates the number of characters in the
  1655.                 receive queue. Valid numbers are 0 to 65534.
  1656.  
  1657.  
  1658.         Return value
  1659.  
  1660.                 ComLenRx returns the number of characters presently
  1661.                 residing in the receive queue.
  1662.  
  1663.  
  1664.         See also
  1665.  
  1666.                 ComFlushRx ComLenTx
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673.  
  1674.                                        27
  1675.  
  1676.  
  1677.  
  1678.         Example
  1679.                 
  1680.                 If there are more than 80 characters available, get a
  1681.                 line.
  1682.  
  1683.                 {
  1684.                 char str[81];
  1685.  
  1686.                    if(ComLenRx() > 80)
  1687.                       puts(ComGets(str, 80, '\n'));
  1688.  
  1689.                 }
  1690.  
  1691.  
  1692.  
  1693.         ----------------------------------------------------------------
  1694.         ComPeek
  1695.         ----------------------------------------------------------------
  1696.  
  1697.         Function
  1698.  
  1699.                 Fetches a copy of the head character in the receive
  1700.                 queue.
  1701.  
  1702.  
  1703.         Syntax
  1704.  
  1705.                 #include "cport.h"
  1706.                 char ComPeek(void);
  1707.  
  1708.  
  1709.         Remarks
  1710.  
  1711.                 ComPeek fetches a copy of the head character in the
  1712.                 receive queue. ComPeek will not remove the character 
  1713.                 from the queue.
  1714.  
  1715.  
  1716.         Return value
  1717.  
  1718.                 ComPeek returns the copy of the head character.
  1719.  
  1720.  
  1721.         See also
  1722.  
  1723.                 ComGetc
  1724.  
  1725.  
  1726.         Example
  1727.  
  1728.                 Determine if the head character is a control character.
  1729.  
  1730.                 #define CTRL_A 1
  1731.                 #define CTRL_Z 26
  1732.                 {
  1733.  
  1734.  
  1735.  
  1736.                                        28
  1737.  
  1738.  
  1739.  
  1740.                 char peek_c;
  1741.                 char str[81];
  1742.  
  1743.                    peek_c = ComPeek();
  1744.  
  1745.                    if(peek_c >= CTRL_A && peek_c <= CTRL_Z)
  1746.                    {
  1747.                       switch(ComGetc())
  1748.                       {
  1749.  
  1750.                         ...command processor
  1751.  
  1752.                       }
  1753.                    }
  1754.  
  1755.                    else
  1756.                       puts(ComGets(str, 80, '\n');
  1757.                    
  1758.                 }
  1759.  
  1760.  
  1761.  
  1762.  
  1763.  
  1764.  
  1765.  
  1766.  
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.  
  1787.  
  1788.  
  1789.  
  1790.  
  1791.  
  1792.  
  1793.  
  1794.  
  1795.  
  1796.  
  1797.  
  1798.                                        29
  1799.  
  1800.  
  1801.  
  1802.         Output Functions
  1803.  
  1804.         Output functions interface with the transmitter and the
  1805.         internal transmit queue. 
  1806.  
  1807.         There are three functions for putting characters/bytes into
  1808.         the transmit queue, all of which return the actual number of
  1809.         characters/bytes successfully put. ComPutc and ComPuts can be
  1810.         used to put control characters as well as normal ascii characters 
  1811.         into the transmit queue. ComOut is intended for putting binary 
  1812.         data into the transmit queue.
  1813.  
  1814.         ComFlushTx flushes the transmitter while ComLenTx finds the 
  1815.         number of characters in the queue.
  1816.  
  1817.  
  1818.  
  1819.  
  1820.  
  1821.  
  1822.  
  1823.  
  1824.  
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.  
  1853.  
  1854.  
  1855.  
  1856.  
  1857.  
  1858.  
  1859.  
  1860.                                        30
  1861.  
  1862.  
  1863.  
  1864.         ----------------------------------------------------------------
  1865.         ComPutc
  1866.         ----------------------------------------------------------------
  1867.  
  1868.         Function 
  1869.         
  1870.                 Puts a character into the transmit queue.
  1871.  
  1872.         Syntax  
  1873.                 #include "cport.h"
  1874.                 int ComPutc(char c);
  1875.  
  1876.  
  1877.         Parameters
  1878.                 
  1879.                 c - Character to be put into the transmit queue.
  1880.  
  1881.  
  1882.         Remarks
  1883.  
  1884.                 ComPutc puts a character into the transmit queue 
  1885.                 to be transmitted. If the transmit queue is full, 
  1886.                 the character is not put and ComPutc returns a 0.
  1887.  
  1888.  
  1889.         Return value    
  1890.         
  1891.                 On success, ComPutc returns a 1. If it fails, it 
  1892.                 returns 0. 
  1893.  
  1894.  
  1895.         See also
  1896.  
  1897.                 ComGetc ComPuts ComOut        
  1898.  
  1899.  
  1900.         Example 
  1901.  
  1902.                 Send an XOFF to the transmitter.
  1903.  
  1904.                 #define XOFF 13
  1905.         
  1906.                 {
  1907.  
  1908.                    if(!ComPutc(XOFF))
  1909.                    {
  1910.  
  1911.                       ...error handler
  1912.  
  1913.                    }
  1914.  
  1915.  
  1916.  
  1917.  
  1918.  
  1919.  
  1920.  
  1921.  
  1922.                                        31
  1923.  
  1924.  
  1925.  
  1926.         ----------------------------------------------------------------
  1927.         ComPuts
  1928.         ----------------------------------------------------------------
  1929.         
  1930.         Function        
  1931.         
  1932.                 Puts a string of characters into the transmit queue.
  1933.  
  1934.  
  1935.         Syntax
  1936.                 
  1937.                 #include "cport.h"
  1938.                 int ComPuts(const char *str);
  1939.  
  1940.  
  1941.         Parameters
  1942.                 
  1943.                 *str  - Pointer to the string to be put into the 
  1944.                         transmit queue.
  1945.  
  1946.  
  1947.         Remarks
  1948.  
  1949.                 ComPuts puts the string 'str' into the transmit queue 
  1950.                 to be transmitted. If the transmit queue becomes full, 
  1951.                 ComPuts will not attempt to put any more characters in 
  1952.                 the queue.
  1953.  
  1954.  
  1955.         Return value    
  1956.         
  1957.                 ComPuts returns the actual number of characters put
  1958.                 into the transmit queue. 
  1959.  
  1960.  
  1961.         See also
  1962.  
  1963.                 ComGets ComPutc ComOut
  1964.  
  1965.  
  1966.         Example
  1967.  
  1968.                 Send the string "hello world" to the transmitter.
  1969.  
  1970.                 {
  1971.                 char *str = "hello world";
  1972.  
  1973.  
  1974.                    if(ComPuts(str) < strlen(str))
  1975.                    {
  1976.  
  1977.                       ...error handler
  1978.  
  1979.                    }
  1980.                 }
  1981.  
  1982.  
  1983.  
  1984.                                        32
  1985.  
  1986.  
  1987.  
  1988.         ----------------------------------------------------------------
  1989.         ComOut
  1990.         ----------------------------------------------------------------
  1991.  
  1992.         Function        
  1993.         
  1994.                 Puts a block of bytes into the transmit queue.
  1995.  
  1996.  
  1997.         Syntax          
  1998.                 #include "cport.h"
  1999.                 unsigned ComOut(const void *abyte, unsigned num_byte);
  2000.  
  2001.  
  2002.         Parameters
  2003.         
  2004.                 *abyte - Pointer to the buffer of the block to be put 
  2005.                          into the transmit queue.
  2006.  
  2007.                 num_byte - Number of bytes to be put into the transmit 
  2008.                            queue.
  2009.  
  2010.  
  2011.         Remarks
  2012.  
  2013.                 ComOut puts the block of bytes pointed to by 'abyte'
  2014.                 into the transmit queue to be transmitted.
  2015.         
  2016.  
  2017.         Return value 
  2018.         
  2019.                 ComOutQ returns the number of bytes actually put into
  2020.                 the transmit queue.
  2021.  
  2022.  
  2023.         See also
  2024.  
  2025.                 ComIn ComPutc ComPuts
  2026.  
  2027.         Example   
  2028.         
  2029.                 Send a block of 128 bytes to the transmitter.
  2030.  
  2031.                 #define BLK_SZ 128
  2032.  
  2033.                 {
  2034.                 byte block[BLK_SZ];
  2035.  
  2036.                      
  2037.                       if(ComOut(block, BLK_SZ) < BLK_SZ)
  2038.                       {
  2039.  
  2040.                          ...error handler
  2041.  
  2042.                       }
  2043.  
  2044.  
  2045.  
  2046.                                        33
  2047.  
  2048.  
  2049.  
  2050.                    }
  2051.                 }
  2052.  
  2053.  
  2054.         ----------------------------------------------------------------
  2055.         ComFlushTx
  2056.         ----------------------------------------------------------------
  2057.  
  2058.         Function
  2059.         
  2060.                 Flushes the transmit queue.
  2061.  
  2062.  
  2063.         Syntax  
  2064.         
  2065.                 #include "cport.h"
  2066.                 void ComFlushTx(void);
  2067.  
  2068.  
  2069.         Remarks
  2070.  
  2071.                 ComFlushTx flushes any existing characters in the
  2072.                 transmit queue.                
  2073.  
  2074.  
  2075.         See also
  2076.  
  2077.                 ComFlushRx ComLenTx
  2078.  
  2079.  
  2080.         Example
  2081.  
  2082.                 If a transmit flush command is in order, flush the
  2083.                 transmitter.
  2084.  
  2085.                 #define ...
  2086.                 #define TX_FLSH 3
  2087.                 #define ...
  2088.                 {
  2089.                 int command;
  2090.        
  2091.                    ...fetch command     
  2092.  
  2093.                    switch(command)
  2094.                    {
  2095.  
  2096.                         case ...:
  2097.                            ...case code
  2098.                            break;
  2099.  
  2100.                         case TX_FLSH:
  2101.                            ComFlushTx();
  2102.                            break;
  2103.  
  2104.                         case ...:
  2105.  
  2106.  
  2107.  
  2108.                                        34
  2109.  
  2110.  
  2111.  
  2112.                            ...case code
  2113.                            break;
  2114.  
  2115.                    }
  2116.                  }
  2117.  
  2118.  
  2119.  
  2120.         ----------------------------------------------------------------
  2121.         ComLenTx
  2122.         ----------------------------------------------------------------
  2123.  
  2124.  
  2125.         Function
  2126.  
  2127.                 Calculates the number of characters in the transmit 
  2128.                 queue.
  2129.  
  2130.  
  2131.         Syntax
  2132.  
  2133.                 #include "cport.h"
  2134.                 unsigned ComLenTx(void);
  2135.  
  2136.  
  2137.         Remarks
  2138.  
  2139.                 ComLenTx calculates the number of characters in the
  2140.                 receive queue. Valid numbers are 0 to 65534.
  2141.  
  2142.  
  2143.         Return value
  2144.  
  2145.                 ComLenTx returns the number of characters presently
  2146.                 in the transmit queue.
  2147.  
  2148.  
  2149.         See also 
  2150.  
  2151.                 ComLenRx ComFlushTx
  2152.  
  2153.  
  2154.         Example
  2155.  
  2156.                 Check if the transmitter has reached 3/4 of its 
  2157.                 capacity.
  2158.  
  2159.                 {
  2160.                    if(ComLenTx() > (TX_SIZE * 3 / 4))
  2161.                    {
  2162.  
  2163.                       ...pause
  2164.  
  2165.                    }
  2166.                 }
  2167.  
  2168.  
  2169.  
  2170.                                        35
  2171.  
  2172.  
  2173.  
  2174.         Status Functions
  2175.  
  2176.         Status functions are for obtaining or setting various status.
  2177.  
  2178.         There are two functions for obtaining status: ComError detects 
  2179.         line errors in the serial port; and ComStatus returns the status 
  2180.         of the input hardware lines.
  2181.  
  2182.         ComDtr, ComRts and ComOut1 manipulate the output hardware
  2183.         lines
  2184.  
  2185.  
  2186.  
  2187.  
  2188.  
  2189.  
  2190.  
  2191.  
  2192.  
  2193.  
  2194.  
  2195.  
  2196.  
  2197.  
  2198.  
  2199.  
  2200.  
  2201.  
  2202.  
  2203.  
  2204.  
  2205.  
  2206.  
  2207.  
  2208.  
  2209.  
  2210.  
  2211.  
  2212.  
  2213.  
  2214.  
  2215.  
  2216.  
  2217.  
  2218.  
  2219.  
  2220.  
  2221.  
  2222.  
  2223.  
  2224.  
  2225.  
  2226.  
  2227.  
  2228.  
  2229.  
  2230.  
  2231.  
  2232.                                        36
  2233.  
  2234.  
  2235.  
  2236.         ----------------------------------------------------------------
  2237.         ComError
  2238.         ----------------------------------------------------------------
  2239.  
  2240.         Function        
  2241.         
  2242.                 Determines if any errors have occurred since the last 
  2243.                 call to ComError.
  2244.  
  2245.  
  2246.         Syntax  
  2247.                 
  2248.                 #include "cport.h"
  2249.                 unsigned ComError(void);
  2250.  
  2251.  
  2252.         Remarks
  2253.  
  2254.                 ComError determines if any errors have occurred since
  2255.                 the last call to ComError or since the start of the
  2256.                 program on the first call to ComError. ComError returns 
  2257.                 an error word in which the bits are defined as follows:
  2258.  
  2259.                         OVERUN  (0x002)  -  overrun error 
  2260.                         PARITY  (0x004)  -  parity error 
  2261.                         FRAMING (0x008)  -  framing error
  2262.                         BREAK   (0x010)  -  break detect 
  2263.                         TIMOUT  (0x080)  -  timeout  
  2264.                         TXFULL  (0x100)  -  transmit queue overflow 
  2265.                         RXFULL  (0x200)  -  receive queue overflow 
  2266.  
  2267.  
  2268.         Return value
  2269.         
  2270.                 If an error has occurred, ComError returns an error
  2271.                 word. If no errors have occurred it returns a 0.
  2272.  
  2273.  
  2274.         See also
  2275.  
  2276.                 ComStatus
  2277.  
  2278.                 
  2279.         Example 
  2280.         
  2281.                 Determine the source of the error if one has occurred.
  2282.  
  2283.                 {
  2284.                 unsigned com_err;
  2285.  
  2286.                    com_err = ComError();
  2287.  
  2288.                    if(com_err)
  2289.                    {
  2290.                         switch(com_err)
  2291.  
  2292.  
  2293.  
  2294.                                        37
  2295.  
  2296.  
  2297.  
  2298.                         {
  2299.  
  2300.                            case OVERUN:
  2301.                               ...over run handler
  2302.                               break;
  2303.  
  2304.  
  2305.                            case PARITY:
  2306.                               ...parity error handler
  2307.                               break;
  2308.  
  2309.                            case ...
  2310.  
  2311.                         }
  2312.                    }
  2313.  
  2314.  
  2315.  
  2316.         ----------------------------------------------------------------
  2317.         ComStatus
  2318.         ----------------------------------------------------------------
  2319.  
  2320.         Function 
  2321.         
  2322.                 Retrieves the status of the hardware lines.   
  2323.  
  2324.  
  2325.         Syntax  
  2326.         
  2327.                 #include "cport.h"
  2328.                 byte ComStatus(void);
  2329.  
  2330.  
  2331.         Remarks
  2332.  
  2333.                 ComStatus retrieves the status of the serial port's 
  2334.                 hardware lines. ComStatus returns a status byte in 
  2335.                 which the bits are defined as follows:
  2336.  
  2337.                         DCTS (0x01) - change in clear to send.
  2338.                         DDSR (0x02) - change in data set ready.
  2339.                         TERI (0x04) - trailing edge ring indicator.
  2340.                         DDCD (0x08) - change in data carrier detect.
  2341.                         CTS  (0x10) - clear to send.
  2342.                         DSR  (0x20) - data set ready.
  2343.                         RI   (0x40) - ring indicator.
  2344.                         DCD  (0x80) - data carrier detect.
  2345.  
  2346.  
  2347.         Return value
  2348.         
  2349.                 ComStatus returns the status byte. 
  2350.  
  2351.  
  2352.  
  2353.  
  2354.  
  2355.  
  2356.                                        38
  2357.  
  2358.  
  2359.  
  2360.         See also
  2361.  
  2362.                 ComError ComRts ComDtr
  2363.  
  2364.  
  2365.         Example 
  2366.         
  2367.                 Test if the modem is powered up and ready.
  2368.  
  2369.                 {
  2370.  
  2371.                    ComDtr(ON);
  2372.                    delay(40);
  2373.  
  2374.  
  2375.                    if(!(ComStatus() & DSR))
  2376.                    {
  2377.                       puts("modem not responding");
  2378.  
  2379.                       ...what to do code
  2380.  
  2381.                    }
  2382.                 }
  2383.  
  2384.  
  2385.  
  2386.         ----------------------------------------------------------------
  2387.         ComRts    
  2388.         ----------------------------------------------------------------
  2389.  
  2390.         Function 
  2391.         
  2392.                 Sets or clears the Request to Send line.
  2393.  
  2394.         Syntax  
  2395.         
  2396.                 #include "cport.h"
  2397.                 void ComRts(byte on_off);
  2398.  
  2399.         Parameters   
  2400.         
  2401.                 on_off - Determines whether RTS is to be set or
  2402.                          cleared. Constants are provided in cport.h.
  2403.                          Possibilities are:
  2404.  
  2405.                          ON  (1) - Sets RTS.
  2406.                          OFF (0) - Clears RTS.
  2407.  
  2408.         Remarks         
  2409.  
  2410.                 ComRts sets or clears the RTS line depending on the
  2411.                 value of 'on_off'. In many cases, this signal will be
  2412.                 looked at by the remote device to determine if it
  2413.                 is safe to transmit. This gives the programmer the 
  2414.                 ability to turn off the remote device's transmitter. 
  2415.  
  2416.  
  2417.  
  2418.                                        39
  2419.  
  2420.  
  2421.  
  2422.  
  2423.                 NOTE: ComRts should never be called when hardware 
  2424.                 handshaking is enabled.
  2425.  
  2426.  
  2427.         See also
  2428.  
  2429.                 ComDtr ComOut1 ComStatus
  2430.  
  2431.  
  2432.         Example
  2433.  
  2434.                 If the receiver is off ('rx_off') but ready to
  2435.                 receive, assert RTS. If it is on but not ready, turn
  2436.                 off RTS.
  2437.  
  2438.                 {
  2439.  
  2440.                    if(rx_off && rx_rdy)
  2441.                       ComRts(ON);
  2442.  
  2443.                    else if(!rx_off && !rx_rdy)
  2444.                       ComRts(OFF);
  2445.                 }
  2446.  
  2447.  
  2448.  
  2449.         ----------------------------------------------------------------
  2450.         ComDtr    
  2451.         ----------------------------------------------------------------
  2452.  
  2453.         Function  
  2454.         
  2455.                 Sets or clears the Data Terminal Ready line.
  2456.  
  2457.  
  2458.         Syntax  
  2459.         
  2460.                 #include "cport.h"
  2461.                 void ComDtr(byte on_off);
  2462.  
  2463.  
  2464.         Parameters 
  2465.         
  2466.                 on_off - Determines whether DTR is to be set or
  2467.                          cleared. Constants are provided in cport.h. 
  2468.                          Possibilities are:
  2469.  
  2470.                          ON  (1) - Sets DTR.
  2471.                          OFF (0) - Clears DTR.
  2472.  
  2473.  
  2474.         Remarks       
  2475.         
  2476.                 ComDtr set or clears the DTR line depending on the
  2477.  
  2478.  
  2479.  
  2480.                                        40
  2481.  
  2482.  
  2483.  
  2484.                 value of 'on_off'. Setting DTR informs the remote
  2485.                 equipment that the computer is ready to establish
  2486.                 communications.
  2487.  
  2488.  
  2489.         See also 
  2490.  
  2491.                 ComRts ComOut1 ComStatus
  2492.  
  2493.  
  2494.         Example 
  2495.  
  2496.                 If an break is detected, turn off DTR.
  2497.  
  2498.                 {
  2499.                 unsigned com_err;
  2500.                 int brk_flg = 0;
  2501.  
  2502.                    com_err = ComError();
  2503.  
  2504.                    if(com_err)
  2505.                    {
  2506.                         switch(com_err)
  2507.                         {
  2508.                            case ...
  2509.  
  2510.                            case BREAK:
  2511.                               ComDtr(OFF);
  2512.                               brk_flg = 1;
  2513.                               break;
  2514.  
  2515.                            case ...
  2516.                         }
  2517.  
  2518.                    }
  2519.                }
  2520.  
  2521.  
  2522.  
  2523.  
  2524.         ----------------------------------------------------------------
  2525.         ComOut1 
  2526.         ----------------------------------------------------------------
  2527.  
  2528.         Function  
  2529.         
  2530.                 Sets or clears the Out 1 line.
  2531.  
  2532.  
  2533.         Syntax  
  2534.         
  2535.                 #include "cport.h"
  2536.                 void ComOut1(byte on_off);
  2537.  
  2538.  
  2539.  
  2540.  
  2541.  
  2542.                                        41
  2543.  
  2544.  
  2545.  
  2546.         Parameters 
  2547.         
  2548.                 on_off - Determines whether OUT1 is to be set or
  2549.                          cleared. Constants are provided in cport.H. 
  2550.                          Possibilities are:
  2551.  
  2552.                          ON  (1) - Sets OUT1.
  2553.                          OFF (0) - Clears OUT1.
  2554.  
  2555.         
  2556.         Remarks         
  2557.                 
  2558.                 ComOut1 sets or clears the general purpose OUT1 line
  2559.                 depending on the value of 'on_off'. OUT1 is a general
  2560.                 purpose output sometimes utilized by device for a
  2561.                 special feature or reset.
  2562.  
  2563.         
  2564.         See also
  2565.  
  2566.                 ComRts ComDtr ComStatus
  2567.  
  2568.  
  2569.         Example 
  2570.  
  2571.                 Reset a smart modem.
  2572.  
  2573.                 #define ComSMSet() ComOut1(ON)
  2574.                 #define ComSMClr() ComOut1(OFF)
  2575.                 {
  2576.                    ComSMRes();
  2577.                    delay(50);
  2578.                    ComSMClr();
  2579.                 }
  2580.  
  2581.  
  2582.  
  2583.  
  2584.  
  2585.  
  2586.  
  2587.  
  2588.  
  2589.  
  2590.  
  2591.  
  2592.  
  2593.  
  2594.  
  2595.  
  2596.  
  2597.  
  2598.  
  2599.  
  2600.  
  2601.  
  2602.  
  2603.  
  2604.                                        42
  2605.  
  2606.  
  2607.  
  2608.         Data Integrity
  2609.  
  2610.         Two functions are currently provided for checking the integrity 
  2611.         of data. They are: ComChecksum and ComCrc16. ComChecksum and 
  2612.         ComCrc16 perform a checksum calculation and a 16 bit cyclic 
  2613.         redundancy check respectively, on a block of bytes. Notice also 
  2614.         that these functions take the same arguments as the functions 
  2615.         ComOut and ComIn.
  2616.  
  2617.  
  2618.  
  2619.  
  2620.  
  2621.  
  2622.  
  2623.  
  2624.  
  2625.  
  2626.  
  2627.  
  2628.  
  2629.  
  2630.  
  2631.  
  2632.  
  2633.  
  2634.  
  2635.  
  2636.  
  2637.  
  2638.  
  2639.  
  2640.  
  2641.  
  2642.  
  2643.  
  2644.  
  2645.  
  2646.  
  2647.  
  2648.  
  2649.  
  2650.  
  2651.  
  2652.  
  2653.  
  2654.  
  2655.  
  2656.  
  2657.  
  2658.  
  2659.  
  2660.  
  2661.  
  2662.  
  2663.  
  2664.  
  2665.  
  2666.                                        43
  2667.  
  2668.  
  2669.  
  2670.         ----------------------------------------------------------------
  2671.         ComChecksum
  2672.         ----------------------------------------------------------------
  2673.         
  2674.         Function
  2675.  
  2676.                 Performs a checksum calculation on a block of bytes.
  2677.  
  2678.  
  2679.         Syntax  
  2680.         
  2681.                 #include "cport.h"
  2682.                 byte ComChecksum(const void *abyte, unsigned n_byte);
  2683.  
  2684.  
  2685.         Parameters
  2686.         
  2687.                 *abyte - Pointer to the block on which the checksum
  2688.                          is to be performed 
  2689.  
  2690.                 num_byte - Number of bytes in the block.
  2691.  
  2692.  
  2693.         Remarks
  2694.  
  2695.                 ComChecksum performs a one byte checksum on the block
  2696.                 of bytes pointed to by 'abyte'. The checksum is
  2697.                 calculated by summing all the bytes in the block and
  2698.                 ignoring the carry outs.
  2699.  
  2700.  
  2701.         Return value
  2702.  
  2703.                 ComChecksum returns the one byte checksum.
  2704.  
  2705.  
  2706.         See also
  2707.  
  2708.                 ComCrc16 ComIn ComOut
  2709.  
  2710.  
  2711.         Example   
  2712.  
  2713.                 Verify the actual checksum matches the received
  2714.                 checksum.
  2715.  
  2716.                 {
  2717.                 struct Xmodem{
  2718.                    byte num;
  2719.                    byte _num;
  2720.                    byte data[128];
  2721.                    byte checksum;
  2722.                    }block;
  2723.                 
  2724.  
  2725.  
  2726.  
  2727.  
  2728.                                        44
  2729.  
  2730.  
  2731.  
  2732.                    ComIn(&block, sizeof(struct Xmodem));
  2733.  
  2734.                    if(ComChecksum(block.data, 128) != block.checksum)
  2735.                    {
  2736.  
  2737.                         ...bad block handler
  2738.  
  2739.                    }
  2740.  
  2741.                    else...
  2742.  
  2743.                 }
  2744.  
  2745.  
  2746.  
  2747.         ----------------------------------------------------------------
  2748.         ComCrc16
  2749.         ----------------------------------------------------------------
  2750.  
  2751.         Function
  2752.  
  2753.                 Performs a 16 bit cyclic redundancy check on a block of 
  2754.                 bytes.
  2755.  
  2756.  
  2757.         Syntax  
  2758.         
  2759.                 #include "cport.h"
  2760.                 unsigned ComCrc16(const void *abyte, unsigned n_byte);
  2761.  
  2762.  
  2763.         Parameters
  2764.         
  2765.                 *abyte - Pointer to the block on which the 16 bit
  2766.                          cyclic redundancy check is to be performed.
  2767.  
  2768.                 num_byte - Number of bytes in the block.
  2769.  
  2770.  
  2771.         Remarks
  2772.  
  2773.                 ComCrc16 performs a 16 bit cyclic redundancy check on 
  2774.                 the block of bytes pointed to by 'abyte'. The 16 bit
  2775.                 CRC is calculated using a table of precalculated
  2776.                 values.
  2777.  
  2778.  
  2779.         Return value
  2780.  
  2781.                 ComCrc16 returns the two byte CRC value.
  2782.  
  2783.  
  2784.         See also
  2785.  
  2786.                 ComChecksum ComIn ComOut
  2787.  
  2788.  
  2789.  
  2790.                                        45
  2791.  
  2792.  
  2793.  
  2794.         Example   
  2795.  
  2796.                 Verify the actual CRC matches the received CRC.
  2797.  
  2798.                 {
  2799.                 struct Xmodem{
  2800.                    byte num;
  2801.                    byte _num;
  2802.                    byte data[128];
  2803.                    unsigned crc16;
  2804.                    }block;
  2805.                 
  2806.  
  2807.                    ComIn(&block, sizeof(struct Xmodem));
  2808.  
  2809.                    if(ComCrc16(block.data, 128) != block.crc16)
  2810.                    {
  2811.  
  2812.                         ...bad block handler
  2813.  
  2814.                    }
  2815.  
  2816.                    else...
  2817.  
  2818.                 }
  2819.  
  2820.  
  2821.  
  2822.  
  2823.  
  2824.  
  2825.  
  2826.  
  2827.  
  2828.  
  2829.  
  2830.  
  2831.  
  2832.  
  2833.  
  2834.  
  2835.  
  2836.  
  2837.  
  2838.  
  2839.  
  2840.  
  2841.  
  2842.  
  2843.  
  2844.  
  2845.  
  2846.  
  2847.  
  2848.  
  2849.  
  2850.  
  2851.  
  2852.                                        46
  2853.  
  2854.  
  2855.  
  2856.         Misc Functions
  2857.  
  2858.         There are two pair of misc functions: ComSetBreak and
  2859.         ComClrBreak set and clear a break condition; ComPutScrtch
  2860.         and ComGetScrtch write to and read from the scratch register.
  2861.  
  2862.                 
  2863.  
  2864.  
  2865.  
  2866.  
  2867.  
  2868.  
  2869.  
  2870.  
  2871.  
  2872.  
  2873.  
  2874.  
  2875.  
  2876.  
  2877.  
  2878.  
  2879.  
  2880.  
  2881.  
  2882.  
  2883.  
  2884.  
  2885.  
  2886.  
  2887.  
  2888.  
  2889.  
  2890.  
  2891.  
  2892.  
  2893.  
  2894.  
  2895.  
  2896.  
  2897.  
  2898.  
  2899.  
  2900.  
  2901.  
  2902.  
  2903.  
  2904.  
  2905.  
  2906.  
  2907.  
  2908.  
  2909.  
  2910.  
  2911.  
  2912.  
  2913.  
  2914.                                        47
  2915.  
  2916.  
  2917.  
  2918.         ----------------------------------------------------------------
  2919.         ComSetBreak
  2920.         ----------------------------------------------------------------
  2921.  
  2922.         Function     
  2923.         
  2924.                 Set a break condition. 
  2925.  
  2926.         Syntax     
  2927.         
  2928.                 #include "cport.h"
  2929.                 void ComSetBreak(void);
  2930.  
  2931.         Remarks 
  2932.  
  2933.                 ComSetBreak sets a break condition. The break condition 
  2934.                 will stay in effect until a subsequent call to
  2935.                 ComClrBreak is made.
  2936.  
  2937.  
  2938.         See also 
  2939.  
  2940.                 ComClrBreak
  2941.  
  2942.  
  2943.         Example
  2944.  
  2945.                 If the an abort condition has been declared, set a
  2946.                 break condition. When it clears, release the break
  2947.                 condition.
  2948.  
  2949.                 {
  2950.                 int abrt_flg;
  2951.                 int brk_flg;
  2952.  
  2953.                    if(abrt_flg && !brk_flg)
  2954.                       ComSetBreak();
  2955.  
  2956.                    else if(!abrt_flg && brk_flg)
  2957.                       ComClrBreak();
  2958.                 }
  2959.  
  2960.  
  2961.  
  2962.         ----------------------------------------------------------------
  2963.         ComClrBreak
  2964.         ----------------------------------------------------------------
  2965.  
  2966.         Function
  2967.         
  2968.                 Clears a previously set break condition.
  2969.  
  2970.  
  2971.  
  2972.  
  2973.  
  2974.  
  2975.  
  2976.                                        48
  2977.  
  2978.  
  2979.  
  2980.         Syntax  
  2981.         
  2982.                 #include "cport.h"
  2983.                 void ClrBreak(void);
  2984.  
  2985.                 
  2986.         Remarks
  2987.  
  2988.                 ComClrBreak clears a previously set break condition.
  2989.  
  2990.  
  2991.         See also
  2992.  
  2993.                 ComSetBreak
  2994.  
  2995.  
  2996.         Example
  2997.  
  2998.                 If the an abort condition has been declared, set a
  2999.                 break condition. When it clears, release the break
  3000.                 condition.
  3001.  
  3002.                 {
  3003.                 int abrt_flg;
  3004.                 int brk_flg;
  3005.  
  3006.                    if(abrt_flg && !brk_flg)
  3007.                       ComSetBreak();
  3008.  
  3009.                    else if(!abrt_flg && brk_flg)
  3010.                       ComClrBreak();
  3011.         
  3012.                 }
  3013.  
  3014.  
  3015.         ----------------------------------------------------------------
  3016.         ComPutScrtch
  3017.         ----------------------------------------------------------------
  3018.  
  3019.         Function
  3020.         
  3021.                 Writes a byte to the serial port's scratch register.
  3022.  
  3023.         Syntax      
  3024.         
  3025.                 #include "cport.h"
  3026.                 void ComPutScrtch(byte abyte);
  3027.  
  3028.  
  3029.         Parameters
  3030.         
  3031.                 abyte - The byte to be written to the scratch register.
  3032.  
  3033.  
  3034.  
  3035.  
  3036.  
  3037.  
  3038.                                        49
  3039.  
  3040.  
  3041.  
  3042.         Remarks 
  3043.         
  3044.                 ComPutScrtch writes the byte value 'abyte' to the
  3045.                 UART scratch register. Not all UARTs have a scratch 
  3046.                 register.
  3047.  
  3048.  
  3049.         See also
  3050.  
  3051.                 ComGetScrtch
  3052.  
  3053.  
  3054.         Example 
  3055.  
  3056.                 Use the scratch register to store a status byte.
  3057.  
  3058.                 #define ComSavStatus() ComPutScrtch(ComStatus())
  3059.                 #define ComOldStatus() ComGetScrtch()
  3060.                 {
  3061.                 unsigned old_stat;
  3062.  
  3063.                    ComSavStatus();
  3064.  
  3065.                    ...later on
  3066.  
  3067.                    old_stat = ComOldStatus();
  3068.                 }
  3069.  
  3070.  
  3071.         ----------------------------------------------------------------
  3072.         ComGetScrtch
  3073.         ----------------------------------------------------------------
  3074.  
  3075.         Function 
  3076.         
  3077.                 Fetches the byte stored in the serial port scratch 
  3078.                 register.
  3079.  
  3080.         Syntax  
  3081.         
  3082.                 #include "cport.h"
  3083.                 byte ComGetScrtch(void);
  3084.  
  3085.  
  3086.         Remarks
  3087.         
  3088.                 ComGetScratch reads the byte in the UART scratch 
  3089.                 register. Not all UARTs have a scratch register.
  3090.  
  3091.  
  3092.         Return value
  3093.  
  3094.                 ComGetScrtch returns the byte read from the UART
  3095.                 scratch register.
  3096.  
  3097.  
  3098.  
  3099.  
  3100.                                        50
  3101.  
  3102.  
  3103.  
  3104.         See also
  3105.  
  3106.                 ComPutScrtch
  3107.  
  3108.  
  3109.         Example 
  3110.  
  3111.                 Use the scratch register to store a status byte.
  3112.  
  3113.                 #define ComSavStatus() ComPutScrtch(ComStatus())
  3114.                 #define ComOldStatus() ComGetScrtch()
  3115.                 {
  3116.                 unsigned old_stat;
  3117.  
  3118.                    ComSavStatus();
  3119.  
  3120.                    ...later on
  3121.  
  3122.                    old_stat = ComOldStatus();
  3123.                 }
  3124.  
  3125.  
  3126.  
  3127.  
  3128.  
  3129.  
  3130.  
  3131.  
  3132.  
  3133.  
  3134.  
  3135.  
  3136.  
  3137.  
  3138.  
  3139.  
  3140.  
  3141.  
  3142.  
  3143.  
  3144.  
  3145.  
  3146.  
  3147.  
  3148.  
  3149.  
  3150.  
  3151.  
  3152.  
  3153.  
  3154.  
  3155.  
  3156.  
  3157.  
  3158.  
  3159.  
  3160.  
  3161.  
  3162.                                        51
  3163.  
  3164.  
  3165.  
  3166.          Appendix A
  3167.  
  3168.  
  3169.         Things to look out for:
  3170.  
  3171.  
  3172.         1) Since Cport is driven by hardware interrupts, input and 
  3173.         output functions will work unpredictably inside a debugger. 
  3174.         If a break point is set and the program is run, everything
  3175.         fuctions until the break point is reached. At that point the 
  3176.         interrupts will probably cease and the transmitter and 
  3177.         receiver will freeze. Needless to say, single-stepping is out 
  3178.         of the question.
  3179.  
  3180.  
  3181.         2) Baud rates above 19200 require special attention because they
  3182.         are sensitive to many factors. Some of these factors are:
  3183.  
  3184.         ∙ Cable length
  3185.         ∙ Machine speed
  3186.         ∙ Type of UART used
  3187.         ∙ Activity of handshaking
  3188.         ∙ Degree of bidirectional transfer (transmitting and receiving
  3189.           simultaneously)
  3190.         ∙ Other interrupt activity
  3191.  
  3192.         The last factor, interrupt activity, includes keyboard
  3193.         activity which can be a major impedance. During critical high
  3194.         speed transfers, some interrupts can be temporarily masked
  3195.         out (disabled) at the PIC to preclude them from impeding the 
  3196.         communications interrupt handler.
  3197.  
  3198.  
  3199.         3) In the small and medium memory models, the near heap where
  3200.         the queue buffers are allocated is limited. The data segment 
  3201.         (globals etc.) + the stack + allocated memory (queue buffers) 
  3202.         must not exceed 64K bytes. If extremely large queue(s) are
  3203.         required, the compact or large model where each queue can be 
  3204.         as large as 65534 bytes must be used.
  3205.  
  3206.         4) The functions ComOpen, ComOpenS and ComClose make calls to 
  3207.         the C functions malloc() and free(). 
  3208.  
  3209.  
  3210.  
  3211.  
  3212.  
  3213.  
  3214.  
  3215.  
  3216.  
  3217.  
  3218.  
  3219.  
  3220.  
  3221.  
  3222.  
  3223.  
  3224.                                        52
  3225.  
  3226.  
  3227.  
  3228.         Appendix B
  3229.  
  3230.  
  3231.         Handshaking schemes
  3232.  
  3233.         There are three options available for implementing handshaking.
  3234.         The first two options are built-in and are enabled by calling
  3235.         the function ComHandshake. The third option is external. The
  3236.         three options are described as follows:
  3237.  
  3238.  
  3239.         Hardware handshaking
  3240.  
  3241.         In hardware handshaking , the hardware signals RTS and CTS
  3242.         are used to achieve handshaking. RTS is used by the receiver 
  3243.         to signal remote device to suspend or resume transmitting. 
  3244.         Similarly, CTS is used by the remote device receiver to signal 
  3245.         the transmitter to suspend or resume transmitting.
  3246.  
  3247.  
  3248.         Software handshaking
  3249.  
  3250.         In software handshaking, the control characters XON (ctrl Q)
  3251.         and XOFF (ctrl S) are used to achieve handshaking. When
  3252.         either the remote device or the computer receive an XOFF
  3253.         character, transmitting is suspended. When an XON character 
  3254.         is received transmitting is resumed. 
  3255.  
  3256.  
  3257.         The receiver
  3258.  
  3259.         When software and/or hardware handshaking are enabled the
  3260.         receiver monitors the length of the receive queue. If the
  3261.         length exceeds the threshold set by the function
  3262.         ComHandshake, the receiver will take the appropriate action
  3263.         to signal the remote device to suspend transmitting. When
  3264.         the length of the receive queue subsequently falls below half 
  3265.         of the threshold, the appropriate action is taken to signal the
  3266.         remote device to resume transmitting.
  3267.  
  3268.  
  3269.         The transmitter
  3270.  
  3271.         If the computer is signaled to suspend transmitting and the
  3272.         corresponding handshaking is enabled, the transmitter will
  3273.         stop transmitting. Subsequently, when the computer is
  3274.         signaled to resume transmitting, the transmitter will pick up
  3275.         where it left off.
  3276.  
  3277.  
  3278.         User handshaking
  3279.  
  3280.         The third option for implementing handshaking is to implement
  3281.         the programmer's own handshaking scheme. To accomplish this, 
  3282.         Cport provides the function ComTx. ComTx enables the programmer 
  3283.  
  3284.  
  3285.  
  3286.                                        53
  3287.  
  3288.  
  3289.  
  3290.         to turn the transmitter on and off. When the transmitter is 
  3291.         turned off, transmitting halts immediately. If any characters 
  3292.         still reside in the transmit queue, they will remain there until 
  3293.         the transmitter is turned back on or the transmit queue is
  3294.         flushed. More characters may be put into the transmit queue 
  3295.         while the transmitter is turned off.
  3296.  
  3297.  
  3298.         Cooperation
  3299.  
  3300.         In any handshaking scheme it is imperative that the computer and 
  3301.         the remote device recognize the handshaking scheme. For instance, 
  3302.         if the remote device recognizes hardware handshaking and the 
  3303.         computer does not, the computer will ignore any requests by the 
  3304.         remote device to suspend transmitting. If the remote device 
  3305.         recognized software handshaking and the computer did not, bogus 
  3306.         XON and XOFF characters might be found to be corrupting the 
  3307.         received data.
  3308.  
  3309.  
  3310.  
  3311.  
  3312.  
  3313.  
  3314.  
  3315.  
  3316.  
  3317.  
  3318.  
  3319.  
  3320.  
  3321.  
  3322.  
  3323.  
  3324.  
  3325.  
  3326.  
  3327.  
  3328.  
  3329.  
  3330.  
  3331.  
  3332.  
  3333.  
  3334.  
  3335.  
  3336.  
  3337.  
  3338.  
  3339.  
  3340.  
  3341.  
  3342.  
  3343.  
  3344.  
  3345.  
  3346.  
  3347.  
  3348.                                        54
  3349.  
  3350.  
  3351.  
  3352.         Appendix C
  3353.  
  3354.  
  3355.         The UART (Universal Asynchronous Receiver/ Transmitter) chip
  3356.  
  3357.  
  3358.         The UART, a single integrated circuit chip, is the backbone 
  3359.         of serial communications in computers today. Serial 
  3360.         communications in IBM and compatible computers is based on the 
  3361.         National Semiconductor family of UARTs. These chips include:         
  3362.         
  3363.                 INS8250
  3364.                 INS8250-B
  3365.                 INS8250A
  3366.                 INS82C50A
  3367.                 NS16450
  3368.                 NS16C450
  3369.                 NS16550A
  3370.  
  3371.  
  3372.         The latest and greatest is the NS16550A which features
  3373.         transmitter and receiver FIFO's. This feature greatly
  3374.         improves performance. Cport will take advantage of the 
  3375.         NS16550 chip in future versions.
  3376.         
  3377.         
  3378.         
  3379.  
  3380.  
  3381.  
  3382.  
  3383.  
  3384.  
  3385.  
  3386.  
  3387.  
  3388.  
  3389.  
  3390.  
  3391.  
  3392.  
  3393.  
  3394.  
  3395.  
  3396.  
  3397.  
  3398.  
  3399.  
  3400.  
  3401.  
  3402.  
  3403.  
  3404.  
  3405.  
  3406.  
  3407.  
  3408.  
  3409.  
  3410.                                        55
  3411.  
  3412.  
  3413.  
  3414.          Appendix D
  3415.  
  3416.  
  3417.         The RS-232 Standard ?!!
  3418.  
  3419.  
  3420.         Voltages, logic levels and pin assignments of the RS-232
  3421.         standard are more or less adhered to. However, the way the
  3422.         RS-232 standard prescribes data transfers to be controlled,
  3423.         more commonly know as handshaking, does not work for computers. 
  3424.         This can lead to much confusion and frustration when trying
  3425.         to connect two serial communications devices from different
  3426.         manufacturers. 
  3427.  
  3428.         The book "The RS-232 Solution" by Joe Campbell addresses 
  3429.         this problem and includes a number of case studies. It is 
  3430.         recommended reading for both the novice and expert programmer. 
  3431.         
  3432.         
  3433.  
  3434.  
  3435.  
  3436.  
  3437.  
  3438.  
  3439.  
  3440.  
  3441.  
  3442.  
  3443.  
  3444.  
  3445.  
  3446.  
  3447.  
  3448.  
  3449.  
  3450.  
  3451.  
  3452.  
  3453.  
  3454.  
  3455.  
  3456.  
  3457.  
  3458.  
  3459.  
  3460.  
  3461.  
  3462.  
  3463.  
  3464.  
  3465.  
  3466.  
  3467.  
  3468.  
  3469.  
  3470.  
  3471.  
  3472.                                        56
  3473.  
  3474.  
  3475.  
  3476.         Appendix E
  3477.  
  3478.  
  3479.         Cport Registration Form
  3480.  
  3481.         Name: ______________________________________________________
  3482.  
  3483.         Company: ___________________________________________________
  3484.  
  3485.         Address: ___________________________________________________
  3486.  
  3487.         City: ____________________________  State: _________________
  3488.         
  3489.         Zip Code: _________________  Phone # : _____________________
  3490.  
  3491.         CompuServe # : _____________________________________________
  3492.  
  3493.         How did you acquire Cport ? _________________________________
  3494.  
  3495.         ____________________________________________________________
  3496.  
  3497.         What functions do you find most useful ?____________________
  3498.  
  3499.         ____________________________________________________________
  3500.  
  3501.         What functions do you find least useful ?___________________
  3502.  
  3503.         ___________________________________________________________
  3504.  
  3505.         What functions, not in Cport, would you like to see ?_______
  3506.  
  3507.         ____________________________________________________________
  3508.  
  3509.         ____________________________________________________________
  3510.  
  3511.         Other comments ?____________________________________________
  3512.  
  3513.         ____________________________________________________________
  3514.  
  3515.  
  3516.         Registration                                      $ 25.00
  3517.  
  3518.         California residents add sales tax ( $ 1.75 )     $________
  3519.  
  3520.         Shipping inside continental U.S.   ( $ 2.00 )
  3521.         Shipping outside continental U.S.  ( $ 5.00 )     $________
  3522.         
  3523.  
  3524.                                                  Total:   $________
  3525.         All Payments Must be in U.S. Dollars
  3526.  
  3527.         Make check or money order payable to:    Bri Productions
  3528.                                                  P.O. Box 7121
  3529.                                                  Fremont, CA 94537-7121
  3530.  
  3531.  
  3532.  
  3533.  
  3534.                                        57
  3535.